package dtu.k12tpm.pbs.util;

import java.io.Reader;
import java.util.ArrayList;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

import dtu.k12tpm.pbs.contact.Address;
import dtu.k12tpm.pbs.contact.Contact;
import dtu.k12tpm.pbs.contact.Email;
import dtu.k12tpm.pbs.contact.IM;
import dtu.k12tpm.pbs.contact.Organization;
import dtu.k12tpm.pbs.contact.Phone;
import dtu.k12tpm.pbs.contact.ClientContact;;

public class MyXStream {
	
	
	//parser tu kieu object sang kieu xml co cau truc
	public static String toXML(ArrayList<Contact> list) {

		XStream writer = new XStream();
		writer.alias("contact", Contact.class);
		writer.aliasAttribute(Contact.class, "checked", "checked");
		writer.aliasAttribute(Contact.class, "id", "ID");
		writer.aliasAttribute(Contact.class, "displayName", "name");
		writer.aliasField("notes", Contact.class, "notes");
		
		writer.alias("phone", Phone.class);
		writer.aliasAttribute(Phone.class, "number", "value");
		writer.aliasAttribute(Phone.class, "type", "type");
		
		writer.alias("email", Email.class);
		writer.aliasAttribute(Email.class, "address", "value");
		writer.aliasAttribute(Email.class, "type", "type");
		
		writer.alias("address", Address.class);
		writer.aliasAttribute(Address.class, "poBox", "pobox");
		writer.aliasAttribute(Address.class, "street", "street");
		writer.aliasAttribute(Address.class, "city", "city");
		writer.aliasAttribute(Address.class, "state", "state");
		writer.aliasAttribute(Address.class, "postalCode", "postal_code");
		writer.aliasAttribute(Address.class, "country", "country");
		writer.aliasAttribute(Address.class, "type", "type");
		
		writer.alias("IM", IM.class);
		writer.aliasAttribute(IM.class, "name", "name");
		writer.aliasAttribute(IM.class, "type", "type");
		
		writer.alias("organization", Organization.class);
		writer.aliasAttribute(Organization.class, "organization", "org");
		writer.aliasAttribute(Organization.class, "positions", "pos");
		writer.aliasAttribute(Organization.class, "type", "type");

		return writer.toXML(list);
	}
	

	
	
/*	
	//parser tu kieu object sang kieu xml co cau truc
	public static String toXML(ArrayList<Contact> ContactList) {

		XStream writer = new XStream();
		writer.alias("contact", ClientContact.class);
//		writer.aliasAttribute(Contact.class, "checked", "checked");
//		writer.aliasAttribute(Contact.class, "id", "ID");
//		writer.aliasAttribute(Contact.class, "displayName", "name");
//		writer.aliasField("notes", Contact.class, "notes");
		
		writer.alias("phone", Phone.class);
//		writer.aliasAttribute(Phone.class, "number", "value");
//		writer.aliasAttribute(Phone.class, "type", "type");
		
		writer.alias("email", Email.class);
//		writer.aliasAttribute(Email.class, "address", "value");
//		writer.aliasAttribute(Email.class, "type", "type");
		
		writer.alias("address", Address.class);
//		writer.aliasAttribute(Address.class, "poBox", "pobox");
//		writer.aliasAttribute(Address.class, "street", "street");
//		writer.aliasAttribute(Address.class, "city", "city");
//		writer.aliasAttribute(Address.class, "state", "state");
//		writer.aliasAttribute(Address.class, "postalCode", "postal_code");
//		writer.aliasAttribute(Address.class, "country", "country");
//		writer.aliasAttribute(Address.class, "type", "type");
		
		writer.alias("IM", IM.class);
//		writer.aliasAttribute(IM.class, "name", "name");
//		writer.aliasAttribute(IM.class, "type", "type");
		
		writer.alias("organization", Organization.class);
//		writer.aliasAttribute(Organization.class, "organization", "org");
//		writer.aliasAttribute(Organization.class, "positions", "pos");
//		writer.aliasAttribute(Organization.class, "type", "type");

		return writer.toXML(ContactList);
	}	
*/	
	
	
	//chuyen tu kieu String xml co cau truc sang object Array List Contact
	@SuppressWarnings("unchecked")
	public static ArrayList<Contact> fromXML(String xml) {
		XStream reader = new XStream(new DomDriver());
		//
		reader.alias("contact", ClientContact.class);
		reader.aliasAttribute(Contact.class, "checked", "checked");
		reader.aliasAttribute(Contact.class, "id", "ID");
		reader.aliasAttribute(Contact.class, "displayName", "name");
		reader.aliasField("notes", Contact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<Contact>) reader.fromXML(xml);
	}

	//chuyen tu kieu file.xml co cau truc sang object Array List Contact
	@SuppressWarnings("unchecked")
	public static ArrayList<Contact> fromXML(Reader xml) {
		XStream reader = new XStream(new DomDriver());
		reader.alias("contact", Contact.class);
		reader.aliasAttribute(Contact.class, "checked", "checked");
		reader.aliasAttribute(Contact.class, "id", "ID");
		reader.aliasAttribute(Contact.class, "displayName", "name");
		reader.aliasField("notes", Contact.class, "notes");
		
		reader.alias("phone", Phone.class);
		reader.aliasAttribute(Phone.class, "number", "value");
		reader.aliasAttribute(Phone.class, "type", "type");
		
		reader.alias("email", Email.class);
		reader.aliasAttribute(Email.class, "address", "value");
		reader.aliasAttribute(Email.class, "type", "type");
		
		reader.alias("address", Address.class);
		reader.aliasAttribute(Address.class, "poBox", "pobox");
		reader.aliasAttribute(Address.class, "street", "street");
		reader.aliasAttribute(Address.class, "city", "city");
		reader.aliasAttribute(Address.class, "state", "state");
		reader.aliasAttribute(Address.class, "postalCode", "postal_code");
		reader.aliasAttribute(Address.class, "country", "country");
		reader.aliasAttribute(Address.class, "type", "type");
		
		reader.alias("IM", IM.class);
		reader.aliasAttribute(IM.class, "name", "name");
		reader.aliasAttribute(IM.class, "type", "type");
		
		reader.alias("organization", Organization.class);
		reader.aliasAttribute(Organization.class, "organization", "org");
		reader.aliasAttribute(Organization.class, "positions", "pos");
		reader.aliasAttribute(Organization.class, "type", "type");

		return (ArrayList<Contact>) reader.fromXML(xml);
	}
	
}
